home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 112 / EnigmaAmiga112CD.iso / dalla rivista / giochi in rete / sorgenti_amislate / flood.c < prev    next >
C/C++ Source or Header  |  1995-08-02  |  3KB  |  118 lines

  1. /* Flood fill routine for use by palette.c.  Generate with a */
  2. /*   dcc -c -gs flood.c  Since this function can eat lots of */
  3. /* stack space!   This doesn't seem to work, though... :(    */
  4.  
  5. #include <stdio.h>
  6. #include <exec/types.h>
  7. #include <clib/graphics_protos.h>
  8. #include <intuition/intuition.h>
  9. #include <libraries/dos.h>
  10.  
  11. #include "palette.h"
  12. #include "amislate.h"
  13. #include "DrawLang.h"
  14. #include "remote.h"
  15. #include "tools.h"
  16. #include "flood.h"
  17.  
  18. extern struct Window * DrawWindow;
  19. extern BOOL BSafeFloods;
  20. extern char * pcWhatToAbort;
  21.  
  22. extern struct PaintInfo PState;
  23.  
  24. BYTE bFloodFromCode = FROM_IDCMP;
  25.  
  26. static BOOL BFloodAbort;
  27.  
  28. /* Recursively flood fills.  Returns the position of the rightmost
  29.    pixel in the current line */
  30.    
  31. /* nLast shows where this instances parent was:
  32.         0 = no parent instance
  33.         1 = parent instance was below us
  34.         -1= parent instance was above us
  35.  
  36.     nOldL, and nOldR give the boundaries of our parent.
  37. */
  38. int FloodFill(int X, int Y, ULONG ulFillThisColor, int nLast, int nOldL, int nOldR, LONG DestCode)
  39. {
  40.     int nReturn;
  41.     pcWhatToAbort = "Flood Fill";
  42.     BFloodAbort = FALSE;
  43.     nReturn = FloodFillAux(X, Y, ulFillThisColor, nLast, nOldL, nOldR, DestCode);
  44.     PState.BPenDown = FALSE;
  45. }    
  46. int FloodFillAux(int X, int Y, ULONG ulFillThisColor, int nLast, int nOldL, int nOldR, LONG DestCode)
  47. {
  48.     int nTemp=X, XLeft=X, XRight=X;
  49.  
  50.     if (BFloodAbort == TRUE) return(0);
  51.     if (CheckForUserAbort() == TRUE) {BFloodAbort = TRUE; return(0); }
  52.  
  53.     /* First we need to see how far left and right the FillThisColor extend */    
  54.     /* keep stepping until we reach side of the screen or end of line */    
  55.     while ((XLeft > DrawWindow->BorderLeft) && 
  56.               (ReadPixel(DrawWindow->RPort,XLeft,Y) == ulFillThisColor))
  57.                     XLeft--;
  58.     XLeft++;
  59.  
  60.     /* keep stepping until we reach side of the screen or end of line */    
  61.     while ((XRight < nGetToolBoxLeft()-2) &&
  62.              (ReadPixel(DrawWindow->RPort,XRight,Y) == ulFillThisColor))
  63.                     XRight++;
  64.     XRight--;
  65.  
  66.     /* Send this line to our peer if we're doing floods with lines */
  67.     if (BSafeFloods == TRUE) OutputAction(bFloodFromCode, MODE_LINE, XLeft, Y, XRight, Y, DestCode);
  68.  
  69.     /* Now draw the line for OUR function! */
  70.     Move(DrawWindow->RPort, XLeft,  Y);
  71.     Draw(DrawWindow->RPort, XRight, Y);
  72.     
  73.     /* Now we go along the line from left to right, issuing FloodFillAuxs for
  74.        every adacent line of the same color we find */
  75.     /* First fill upwards--if we're not at the top!  */
  76.     /* ... and if we didn't just come down from a line that is wider than us! */
  77.     if ((Y >= DrawWindow->BorderTop +2) &&
  78.         ((nLast != -1) || (nOldL > XLeft) || (nOldR < XRight)))
  79.     {
  80.         nTemp = XLeft;
  81.         while (nTemp <= XRight)
  82.         {
  83.             if (ReadPixel(DrawWindow->RPort, nTemp, Y-1) == ulFillThisColor)
  84.                 nTemp = FloodFillAux(nTemp, Y-1, ulFillThisColor, 1, XLeft, XRight, DestCode) + 1;
  85.             else
  86.                 nTemp++;
  87.             if (BFloodAbort == TRUE) return(0);
  88.         }
  89.     }
  90.     
  91.     /* Now fill downwards -- if we're not at the bottom! */
  92.     /* ... and if we didn't just come up from a line that is wider than us! */
  93.     if ((Y >= nGetDrawWindowBottom()) ||
  94.          ((nLast == 1) && (nOldL <= XLeft) && (nOldR >= XRight)))
  95.             return(XRight);
  96.             
  97.     nTemp = XLeft;
  98.     while (nTemp <= XRight)
  99.     {
  100.         if (ReadPixel(DrawWindow->RPort, nTemp, Y+1) == ulFillThisColor)
  101.             nTemp = FloodFillAux(nTemp, Y+1, ulFillThisColor, -1, XLeft, XRight, DestCode) + 1;
  102.         else
  103.             nTemp++;
  104.         if (BFloodAbort == TRUE) return(0);
  105.     }
  106.                         
  107.     return(XRight);
  108. }
  109.                  
  110.  
  111. /* Memory panic! */
  112. void stack_abort(void)
  113. {
  114.     CleanExit(RETURN_ERROR);
  115. }
  116.  
  117.  
  118.